package com.houtai.service.activity;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.Page;
import com.houtai.base.ResultVo;
import com.houtai.bean.TdActivityComments;
import com.houtai.bean.User;
import com.houtai.bean.dto.avtivity.TdActivityListDto;
import com.houtai.bean.dto.avtivity.comments.TdActivityCommentsInsertDto;
import com.houtai.bean.dto.avtivity.comments.TdActivityCommentsListDto;
import com.houtai.bean.vo.activity.*;
import com.houtai.bean.vo.activity.comments.TdActivityCommentsList;
import com.houtai.bean.vo.activity.comments.TdActivityCommentsListVo;
import com.houtai.bean.vo.activity.comments.TdActivityCommentsReplyList;
import com.houtai.common.util.WordsUtil;
import com.houtai.dao.TdActivityCommentsDao;
import com.houtai.dao.TdActivityDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityService {
    private static Logger logger = Logger.getLogger(ActivityService.class);
    @Autowired
    private TdActivityDao tdActivityDao;
    @Autowired
    private TdActivityCommentsDao tdActivityCommentsDao;
    @Autowired
    private RedisTemplate redisTemplate;
  
    /**
     * @Purpose:    活动列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public TdActivityListVo activityList(TdActivityListDto dto) {
        String logInfo = this.getClass().getName() + ":activityList:";
        TdActivityListVo retInfo = new TdActivityListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            String orderBy = "activity_id DESC";
            if (dto.getIs_new() == 0) {
                orderBy = "start_time desc";
            }
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<TdActivityList> lists = tdActivityDao.findTdActivityList(dto);
            PageInfo<TdActivityList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无活动");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    活动评论
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ResultVo
     */
    public synchronized ResultVo activityComments(TdActivityCommentsInsertDto dto, String token) {
        String logInfo = this.getClass().getName() + ":articleComments:";
        ResultVo retInfo = new ResultVo();
        logger.info("======" + logInfo + "begin======");
        try {
            TdActivityDetail tdActivityDetail = tdActivityDao.findTdActivityDetail(dto.getActivity_id());
            if (tdActivityDetail != null) {
                if (WordsUtil.isExit(dto.getComments_content())) {
                    User user = (User)redisTemplate.opsForValue().get(token);
                    TdActivityComments comments = new TdActivityComments();
                    comments.setActivity_id(dto.getActivity_id());
                    comments.setComments_content(dto.getComments_content());
                    comments.setIs_reply(dto.getComments_id());
                    comments.setUser_id(user.getUser_id());
                    tdActivityCommentsDao.insertTdActivityComments(comments);
                    if (dto.getComments_id() == 0) {
                        tdActivityDetail.setComments_size(tdActivityDetail.getComments_size() + 1);
                        tdActivityDao.updateTdActivityCommentsSize(tdActivityDetail);
                    }
                    retInfo.setMark("0");
                    retInfo.setTip("成功");
                } else {
                    retInfo.setMark("1");
                    retInfo.setTip("评论包含敏感词");
                }
            } else {
                retInfo.setMark("1");
                retInfo.setTip("活动不存在");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    活动评论列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     ArticleListVo
     */
    public TdActivityCommentsListVo activityCommentsList(TdActivityCommentsListDto dto) {
        String logInfo = this.getClass().getName() + ":commentsList:";
        TdActivityCommentsListVo retInfo = new TdActivityCommentsListVo();
        logger.info("======" + logInfo + "begin======");
        try {

            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), "comments_id DESC");
            List<TdActivityCommentsList> lists = tdActivityCommentsDao.findTdActivityCommentsList(dto);
            PageInfo<TdActivityCommentsList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {
                for (int i = 0; i < list.getList().size(); i ++){
                    List<TdActivityCommentsReplyList> reply = tdActivityCommentsDao.findTdActivityCommentsReplyList(list.getList().get(i).getComments_id());
                    list.getList().get(i).setReplys(reply);
                }

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无评论");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose:    我的活动列表
     * @param:      dto
     * @Version:    1.0
     * @Author:     li
     * @time:       2019/8/21
     * @return:     UserActivityListVo
     */
    public UserActivityListVo userActivityList(Page dto, String token) {
        String logInfo = this.getClass().getName() + ":userActivityList:";
        UserActivityListVo retInfo = new UserActivityListVo();
        logger.info("======" + logInfo + "begin======");
        try {
            User user = (User)redisTemplate.opsForValue().get(token);
            String orderBy = "apply_id DESC";
            PageHelper.startPage(dto.getCurrent_page(), dto.getPage_size(), orderBy);
            List<UserActivityList> lists = tdActivityDao.findUserActivityList(user.getUser_id());
            PageInfo<UserActivityList> list = new PageInfo<>(lists);
            if (list.getTotal() > 0) {

                retInfo.setCurrent_page(dto.getCurrent_page());
                retInfo.setList(list.getList());
                retInfo.setPage_size(list.getPageSize());
                retInfo.setTotal_record(list.getTotal());
                retInfo.setPages(list.getPages());
                retInfo.setMark("0");
                retInfo.setTip("查询成功");
            } else {
                retInfo.setMark("1");
                retInfo.setTip("无已报名活动列表");
            }

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
